<?php

require( "functions.php" );

$topNode = LoggerCreateXMLDocument();

if ( LoggerOpenDB() )
{
	if ( isset( $_POST[ "name" ], $_POST[ "logger_id" ], $_POST[ "update_interval" ], $_POST[ "frequency" ] ) )
	{
		$name = mysql_real_escape_string( rawurldecode( $_POST[ "name" ] ) );
		$logger_id = mysql_real_escape_string( rawurldecode( $_POST[ "logger_id" ] ) );
		$update_interval = ( int ) $_POST[ "update_interval" ];
		$frequency = mysql_real_escape_string( rawurldecode( $_POST[ "frequency" ] ) );
		if ( isset( $_POST[ "activation_time" ] ) )
		{
			$activation_time = "'" . mysql_real_escape_string( rawurldecode( $_POST[ "activation_time" ] ) ) . "'";
		}
		else
		{
			$activation_time = "NULL";
		}
		if ( mysql_query( "START TRANSACTION" ) )
		{
			$sql = "INSERT INTO nets ( name, logger_id, frequency, activation_time ) values ( '$name', '$logger_id', '$frequency', $activation_time )";
			if ( mysql_query( $sql ) && mysql_affected_rows() == 1 )
			{
				$id = mysql_insert_id();
				$sql = "INSERT INTO loggers ( net_id, logger_id, update_interval ) values ( $id, '$logger_id', $update_interval )";
				if ( mysql_query( $sql ) && mysql_affected_rows() == 1 )
				{
					$sql = "SELECT * FROM loggers WHERE net_id = $id AND logger_id = '$logger_id'";
					LoggerAppendSelect( $sql, "loggers", $topNode );
				}
				else
				{
					$error = "INSERT INTO loggers failure";
				}
			}
			else
			{
				$error = "INSERT INTO nets failure";
			}
		}
		else
		{
			$error = "START TRANSACTION failure";
		}
		
		if ( isset( $error ) )
		{
			mysql_query( "ROLLBACK" );
		}
		else
		{
			mysql_query( "COMMIT" );
		}
	}
	else if ( isset( $_POST[ "net_id" ], $_POST[ "logger_id" ], $_POST[ "update_interval" ] ) )
	{
		$net_id = ( int ) $_POST[ "net_id" ];
		$logger_id = mysql_real_escape_string( rawurldecode( $_POST[ "logger_id" ] ) );
		$update_interval = ( int ) $_POST[ "update_interval" ];
		$sql = "INSERT INTO loggers ( net_id, logger_id, update_interval ) values ( $net_id, '$logger_id', $update_interval )";
		$success = ( mysql_query( $sql ) && mysql_affected_rows() == 1 );
		if ( !$success )
		{
			$sql = "UPDATE loggers SET update_count = update_count + 1 WHERE net_id = $net_id AND logger_id = '$logger_id'";
			$success = ( mysql_query( $sql ) && mysql_affected_rows() == 1 );
		}
		if ( $success )
		{
			$sql = "SELECT * FROM nets WHERE net_id = $net_id";
			LoggerAppendSelect( $sql, "nets", $topNode );
			$sql = "SELECT * FROM loggers WHERE net_id = $net_id";
			LoggerAppendSelect( $sql, "loggers", $topNode );
			$sql = "SELECT * FROM roster WHERE net_id = $net_id";
			LoggerAppendSelect( $sql, "roster", $topNode );
			$sql = "SELECT * FROM activity WHERE net_id = $net_id";
			LoggerAppendSelect( $sql, "activity", $topNode );
		}
		else
		{
			$error = "INSERT INTO loggers failure";
		}
	}
	else if ( isset( $_POST[ "net_id" ], $_POST[ "logger_id" ], $_POST[ "activation_time" ] ) )
	{
		$net_id = ( int ) $_POST[ "net_id" ];
		$logger_id = mysql_real_escape_string( rawurldecode( $_POST[ "logger_id" ] ) );
		$activation_time = mysql_real_escape_string( rawurldecode( $_POST[ "activation_time" ] ) );
		$sql = "UPDATE nets SET activation_time = '$activation_time' WHERE net_id = $net_id AND logger_id = '$logger_id' AND is_active = 1 AND activation_time IS NULL";
		if ( mysql_query( $sql ) == FALSE || mysql_affected_rows() != 1 )
		{
			$error = "Net activation time cannot be set by this client";
		}
	}
	else if ( isset( $_POST[ "net_id" ], $_POST[ "logger_id" ], $_POST[ "deactivation_time" ] ) )
	{
		$net_id = ( int ) $_POST[ "net_id" ];
		$logger_id = mysql_real_escape_string( rawurldecode( $_POST[ "logger_id" ] ) );
		$deactivation_time = mysql_real_escape_string( rawurldecode( $_POST[ "deactivation_time" ] ) );
		$sql = "UPDATE nets SET deactivation_time = '$deactivation_time', is_active = 0 WHERE net_id = $net_id AND logger_id = '$logger_id' AND " .
			"is_active = 1 AND activation_time IS NOT NULL AND deactivation_time IS NULL";
		if ( mysql_query( $sql ) == FALSE || mysql_affected_rows() != 1 )
		{
			$error = "Net deactivation time cannot be set by this client";
		}
	}
	else if ( isset( $_POST[ "net_id" ], $_POST[ "frequency" ] ) )
	{
		$net_id = ( int ) $_POST[ "net_id" ];
		$frequency = mysql_real_escape_string( rawurldecode( $_POST[ "frequency" ] ) );
		$sql = "UPDATE nets SET frequency = '$frequency' WHERE net_id = $net_id AND is_active = 1";
		if ( mysql_query( $sql ) == FALSE || mysql_affected_rows() != 1 )
		{
			$error = "Frequency cannot be set by this client";
		}
	}
	else
	{
		$sql = "SELECT * FROM nets WHERE is_active = 1 ORDER BY net_id";
		LoggerAppendSelect( $sql, "nets", $topNode );
		$sql = "SELECT * FROM former_members";
		LoggerAppendSelect( $sql, "former_members", $topNode );
		$sql = "SELECT * FROM valid_callsigns WHERE is_local = 1";
		LoggerAppendSelect( $sql, "valid_callsigns", $topNode );
	}
}
else
{
	$error = "DB open failure";
}

if ( isset( $error ) )
{
	LoggerAppendError( $topNode, $error );
}

$doc = $topNode->ownerDocument;
echo $doc->saveXML();

?>